package edu.cth.scrabbit.shared;

import java.io.Serializable;

/**
 * A class that contains the information about the letters in the game,
 * including their eventual position on the game board
 * 
 * @author Marcus Harryson, Anders Jonsson
 * 
 */
public class GameLetter implements Serializable {
	private static final long serialVersionUID = 62835416231579538L;
	private char letter;
	private int yIndex = -1;
	private int xIndex = -1;

	public GameLetter() {
		this(' ');
	}

	public GameLetter(char letter) {
		this.letter = Character.toUpperCase(letter);
	}

	public GameLetter(GameLetter letter) {
		this.letter = letter.getLetter();
		if (letter.getYIndex() != -1 && letter.getXIndex() != -1) {
			this.yIndex = letter.getYIndex();
			this.xIndex = letter.getXIndex();
		}
	}

	public char getLetter() {
		return letter;
	}

	public boolean equals(GameLetter other) {
		if (other == null) {
			return false;
		}

		if (!(other instanceof GameLetter)) {
			return false;
		}

		if (this.letter == other.letter) {
			return true;
		} else {
			return false;
		}
	}

	public boolean isDummy() {
		return letter == ' ';
	}

	public void setPosition(int xIndex, int yIndex) {
		this.yIndex = yIndex;
		this.xIndex = xIndex;
	}

	public int getYIndex() {
		return this.yIndex;
	}

	public int getXIndex() {
		return this.xIndex;
	}

	@Override
	public String toString() {
		if (!this.isDummy()) {
			return letter + "-" + GameLetterUtils.getPoints(letter);
		} else {
			return "";
		}
	}

	@Override
	public GameLetter clone() {
		GameLetter letter = new GameLetter(this.letter);
		if (!(yIndex == -1 || xIndex == -1)) {
			letter.setPosition(yIndex, xIndex);
		}
		return letter;
	}

	public boolean hasPosition(int x, int y) {
		return this.yIndex == y && this.xIndex == x;
	}
}
